/*
 * MyCenter.cpp
 *
 *  Created on: 2013-2-4
 *      Author: Administrator
 */

#include "MyCenter.h"


MyCenter::MyCenter(const QString &uname, const quint32 &uid, QWidget *parent):QWidget(parent), modifyPswButton("修改密码"), exitButton("退出系统"),
myProjectLabel("我管理的项目"), projectOnLabel("进行中的项目"), budgetMakeButton("制定预算计划"),
budgetViewButton("查看预算计划"),projectFinishedLabel("已完结的项目"),
budgetFinishedViewButton("查看预算计划"), tableOn(100, 5), tableFinished(100, 5)
{
	this->uid = uid;
	welcomeLabel.setText(QString("尊敬的%1您好，欢迎回来。").arg(uname));
	db = Dao.getDateBase();
	initUi();
	initTableOn();
	initTableFinished();
}

void MyCenter::initUi()
{
	tableFinished.setAutoFillBackground(true);
	QStringList finishedHeader;
	finishedHeader << "项目号" << "项目名称" << "项目总预算" << "结束时间" << "开始时间";
	tableFinished.setHorizontalHeaderLabels(finishedHeader);
	tableFinished.setColumnHidden(0, true);
	tableFinished.setSelectionBehavior(QAbstractItemView::SelectRows);
	tableFinished.setSelectionMode(QAbstractItemView::SingleSelection);
	tableFinished.setEditTriggers(QAbstractItemView::NoEditTriggers);
	QStringList onHeader;
	onHeader << "" << "" << "" << "" << "" << "";
	tableOn.setHorizontalHeaderLabels(onHeader);
	tableOn.setColumnHidden(0, true);
	tableOn.setSelectionBehavior(QAbstractItemView::SelectRows);
	tableOn.setSelectionMode(QAbstractItemView::SingleSelection);
	tableOn.setEditTriggers(QAbstractItemView::NoEditTriggers);
	layout.addWidget(&welcomeLabel, 0, 0);
	layout.addWidget(&modifyPswButton, 0, 2);
	layout.addWidget(&exitButton, 0, 3);
	layout.addWidget(&myProjectLabel, 1, 0);
	layout.addWidget(&projectOnLabel, 2, 0);
	layout.addWidget(&budgetMakeButton, 2, 2);
	layout.addWidget(&budgetViewButton, 2, 3);
	layout.addWidget(&tableOn, 3, 0, 3, 4);
	layout.addWidget(&projectFinishedLabel, 6, 0);
	layout.addWidget(&budgetFinishedViewButton, 6, 3);
	layout.addWidget(&tableFinished, 7, 0, 3, 4);
	this->setLayout(&layout);
	setWindowTitle("个人中心");

	connect(&modifyPswButton, SIGNAL(clicked()), this, SLOT(slt_setMode()));
	connect(&modifyPswd, SIGNAL(sig_modifyOrAdd(const int &, const int &, const QString &, const QString &, const int &, const bool &, const QString &)),
				this, SLOT(slt_modifyPsw(const int &, const int &, const QString &, const QString &, const int &, const bool &, const QString &)));
}

void MyCenter::initTableOn()
{

}

void MyCenter::initTableFinished()
{
	qDebug() << "enter MyCenter::init()";
	QSqlQuery query(db);
	query.exec("SELECT id, name, totalBuget, startDate, finishDate FROM project WHERE finished = true");
	QSqlRecord record = query.record();
	int idx_id = record.indexOf("id");
	int idx_name = record.indexOf("name");
	int idx_totalBuget = record.indexOf("totalBuget");
	int idx_startDate = record.indexOf("startDate");
	int idx_finishDate = record.indexOf("finishDate");
	int i = 0;
	while(query.next())
	{
		tableFinished.setItem(i, idx_id, new QTableWidgetItem(query.value(idx_id).toString()));
		tableFinished.setItem(i, idx_name, new QTableWidgetItem(query.value(idx_name).toString()));
		tableFinished.setItem(i, idx_totalBuget, new QTableWidgetItem(query.value(idx_totalBuget).toString()));
		tableFinished.setItem(i, idx_startDate, new QTableWidgetItem(query.value(idx_startDate).toString()));
		tableFinished.setItem(i, idx_finishDate, new QTableWidgetItem(query.value(idx_finishDate).toString()));
		if(++i > tableFinished.rowCount())
			tableFinished.insertRow(tableFinished.rowCount());
	}

}

void MyCenter::slt_setMode()
{
	modifyPswd.setMode(ModifyUser::modifyMode);
	QSqlQuery query(db);
	query.prepare("SELECT * FROM user WHERE id=:ID");
	query.bindValue(":ID", uid);
	query.exec();
	modifyPswd.setMode(ModifyUser::modifyMode);
	QSqlRecord record = query.record();
	int idx_name = record.indexOf("name");
	int idx_signName = record.indexOf("signName");
	int idx_type = record.indexOf("type");
	int idx_locked = record.indexOf("locked");
	int idx_password = record.indexOf("password");
	if(query.next())
	{
		modifyPswd.setUi(uid, query.value(idx_name).toString(), query.value(idx_signName).toString(),
				query.value(idx_type).toInt(), query.value(idx_locked).toBool(), query.value(idx_password).toString());
	}
	modifyPswd.show();
}
void MyCenter::slt_modifyPsw(const int &mode, const int &id, const QString &name, const QString &signName,
		const int &type, const bool &locked, const QString &password)
{
	qDebug() << "enter UserManager::slt_modifyOrAdd" << "; id = " << id << "; name is " << name
			<< "; signname is " << signName << "; type is " << type << "; locked is " << locked
			<< "; passeord is " << password;
	QSqlQuery queryTest(db);
	queryTest.prepare("SELECT signName FROM user WHERE id<>:Id");
	queryTest.bindValue(":Id", id);
	queryTest.exec();
	bool signNameExisted = false;
	while(queryTest.next())
	{
		if(queryTest.value(0).toString() == signName)
		{
			signNameExisted = true;
			break;
		}
	}
	qDebug() << "signNameExisted = " << signNameExisted;
	if(signName == 0 || signNameExisted)
	{
		int r = QMessageBox::information(this, tr("登录名错误"),
				tr("你输入的登录名已存在或者为空！"), QMessageBox::Yes);
		if(r == QMessageBox::Yes)
		{
			modifyPswd.show();
			return ;
		}
	}


	qDebug() << "modifying......" << "locked = " << locked;
	QSqlQuery query(db);
	query.prepare("UPDATE user SET signName=:SignName, password=:Password WHERE id=:Id");
	query.bindValue(":Id", id);
	query.bindValue(":SignName", signName);
	query.bindValue(":Password", password);
	query.exec();
}

MyCenter::~MyCenter() {
	// TODO Auto-generated destructor stub
}

